<?php

class UserModule extends CWebModule
{
    public $loginUrl = array("/user/login");
	public function init()
	{
		// this method is called when the module is being created
		// you may place code here to customize the module or the application

		// import the module-level models and components
		$this->setImport(array(
			'user.models.*',
			'user.components.*',
		));
	}

    public static function t($str='',$params=array(),$dic='user') {
		return Yii::t("UserModule.".$dic, $str, $params);
	}

	public function beforeControllerAction($controller, $action)
	{
		if(parent::beforeControllerAction($controller, $action))
		{
			// this method is called before any module controller action is performed
			// you may place customized code here
			return true;
		}
		else
			return false;
	}
	public function createUserlogs($action_table,$action_id_table,$type)
    {
    	//action_table la ten cua bảng dữ liệu
    	//action_id_table la id của bảng đó
    	//type là hành động cập nhật: create là 1,update là hai,xóa là 3
        $sql ="insert into tbl_user_logs(user_id,action_table,action_id_table,ip_user,type) values
            (:uid,:action_table,:action_id_table,:ip_user,:type)";
            $command = Yii::app()->db->createCommand($sql);
            $command->bindParam(":uid",Yii::app()->user->id,PDO::PARAM_INT);
            $command->bindParam(":action_id_table",$action_id_table,PDO::PARAM_INT);
            $command->bindParam(":action_table",$action_table,PDO::PARAM_STR);
            $command->bindParam(":ip_user",CHttpRequest::getUserHostAddress(),PDO::PARAM_STR);
            $command->bindParam(":type",$type,PDO::PARAM_INT);
            $command->execute();
    }
    public function getJobDoing($status)
    {
        $model = Users::model()->findByPk(Yii::app()->user->id);
    	$count = 0;
    	foreach ($model->bidjobs as $key) {
    		if ($key->job->status == $status){
    			$count++;
    		}
    	}
    	return $count;
    }

    public function getInboxView()
    {
        $user = Yii::app()->user->id;
        $data = Inbox::model()->findAll("status = 0 and user_id = $user");
        return count($data) ;
    }

    public function getCountJobtype($jobtype_id) {
        $count= 0;
        $skil= UserSkill::model()->findAll("user_id=:id and skill_id != 0",array(':id'=>Yii::app()->user->id));
        foreach ($skil as $key) {
            if($key->skill->subjobtype->jobtype->id == $jobtype_id){
                $count++;
            }
        }
        return $count;
    }
}
